package com.vogue.flowmeter.profile.data;

import android.util.Log;

import com.vogue.flowmeter.util.ConvertUtils;

import no.nordicsemi.android.ble.data.Data;

/**
 * 读指令 协议封装
 */
public class VogueBleReadData {
    //特殊读指令
    //读软件版本
    private static final byte READ_VERSION = 0x08;

    //各种读操作
    //读设备PID
    private static final byte READ_EQUIPMENT_PID = 0x10;
    //读卡号
    private static final byte READ_SYSTEM_CARD_NUMBER = 0x14;
    //读设备MasterIP
    private static final byte READ_EQUIPMENT_MASTERIP = 0x15;
    //读SubIP
    private static final byte READ_EQUIPMENT_SUBIP = 0x16;
    //读历史水量记录
    private static final byte READ_HISTORY_WATER_QUANTITY = 0x19;
    //读冻结水量记录
    private static final byte READ_HFROZEN_WATER_QUANTITY = 0x1a;

    //时间类读取操作
    //读采样周期
    private static final byte READ_SAMPLING_PERIOD = 0x20;
    //读发送周期
    private static final byte READ_SENDING_PERIOD = 0x21;
    //读日历时钟
    private static final byte READ_CALENDAR_CLOCK = 0x22;


    //读IMEI
    private static final byte READ_IMEI = 0x40;
    //读IMSI
    private static final byte READ_IMSI = 0x41;
    //读USIM ID
    private static final byte READ_USIM_ID = 0x42;
    //读信号强度CSQ
    private static final byte READ_CSQ = 0x43;


    public static Data ReadVersion() {
        return new Builder().setControl(READ_VERSION).builder();
    }



    public static Data ReadEquipmentPid() {
        return new Builder().setControl(READ_EQUIPMENT_PID).builder();
    }

    public static Data ReadSystemCardNumber() {
        return new Builder().setControl(READ_SYSTEM_CARD_NUMBER).builder();
    }

    public static Data ReadEquipmentMasterIp() {
        return new Builder().setControl(READ_EQUIPMENT_MASTERIP).builder();
    }

    public static Data ReadEquipmentSubIp() {
        return new Builder().setControl(READ_EQUIPMENT_SUBIP).builder();
    }

    public static Data ReadHistoryWaterQuantity() {
        return new Builder().setControl(READ_HISTORY_WATER_QUANTITY).builder();
    }

    public static Data ReadFrozenWaterQuantity() {
        return new Builder().setControl(READ_HFROZEN_WATER_QUANTITY).builder();
    }

    public static Data ReadSamplingPeriod() {
        return new Builder().setControl(READ_SAMPLING_PERIOD).builder();
    }

    public static Data ReadSendingPeriod() {
        return new Builder().setControl(READ_SENDING_PERIOD).builder();
    }

    public static Data ReadCalendarClock() {
        return new Builder().setControl(READ_CALENDAR_CLOCK).builder();
    }

    public static Data ReadImei() {
        return new Builder().setControl(READ_IMEI).builder();
    }

    public static Data ReadImsi() {
        return new Builder().setControl(READ_IMSI).builder();
    }

    public static Data ReadUsimId() {
        return new Builder().setControl(READ_USIM_ID).builder();
    }

    public static Data ReadCsq() {
        return new Builder().setControl(READ_CSQ).builder();
    }


    /**
     * 协议构造器
     */
    public static class Builder {

        //帧头
        private String header;
        //设备标识
        private String identification;
        //传输方向
        private String direction;
        //地址
        private String address;
        //加密版本
        private String encryption;
        //控制字
        private String control;
        //数据长度
        private String dataLength;
        //数据
        private String data;
        //校验和
        private String checksum;
        //帧尾
        private String end;


        public Builder() {
            Builder();
        }

        private Builder Builder() {

            this.header = Integer.toHexString(0x68);
            this.identification = Integer.toHexString(0x62) + Integer.toHexString(0x74);
            this.direction = Integer.toHexString(0xaa);
            this.address = Integer.toHexString(0xff);
            this.encryption = "00";
            this.end = Integer.toHexString(0x16);

            return this;
        }

        public Builder setControl(int command) {
            this.control = String.format("%02x", command);
            this.data = "";
            this.dataLength = "0000";
            String bleData = header + identification + direction + address + encryption + control + dataLength + data;
            this.checksum = checkSum(bleData);
            return this;
        }

        public Data builder() {
            String read = header + identification + direction + address + encryption + control + dataLength + data + checksum + end + "\\r\\n";
            return Data.from(read);
        }

        private String checkSum(String data) {

            Log.e("checkSum", data + " read " + ConvertUtils.makeCheckSum(data));

            return ConvertUtils.makeCheckSum(data);
        }

    }

}
